package SharedCloud
{
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	public class Cache
	{
		protected var innerCache:Dictionary = new Dictionary();
		
		public function add(obj:SyncedObject):void
		{
			if(!innerCache[obj.clientID])
				innerCache[obj.clientID] = new Dictionary();
			innerCache[obj.clientID][obj.localID] = obj;
		}
		
		public function contains(obj:SyncedObject):Boolean
		{
			var found:SyncedObject = findActual(obj);
			if(found && found == obj)
				return true;
			return false;
		}
		
		public function find(clientID:int, localID:int):SyncedObject
		{
			if(innerCache[clientID])
				return innerCache[clientID][localID];
			return null;
		}
		public function findRef(ref:Ref):SyncedObject
		{
			return find(ref.clientID, ref.localID);
		}
		public function findActual(obj:SyncedObject):SyncedObject
		{
			return find(obj.clientID, obj.localID);
		}
		
		public function findAllOfType(type:Class):ArrayCollection
		{
			var found:ArrayCollection = new ArrayCollection();
			for each(var lidDict:Dictionary in innerCache)
			{
				for each(var so:Object in lidDict)
				{
					if(so is type)
						found.addItem(so);
				}
			}
			return found;
		}
	}
}